/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.display.gui;

import deatax.Resource;
import deatax.ResourceAmount;
import deatax.Settlement;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;
import javax.swing.JPanel;

/**
 *
 * @author pson
 */
public class ResourcesPanel extends JPanel {

    private int padding = 10;
    private static final int ICON_WIDTH = 20;
    private static final int TEXT_WIDTH = 25;
    private static final int PREF_HEIGHT = 30;
    protected ArrayList<ResourceAmount> resources;
    protected int population = -1;

    public ResourcesPanel() {
        resources = new ArrayList<ResourceAmount>();

        this.setPreferredSize(new Dimension(200, PREF_HEIGHT));
    }
    
    public void setPadding(int n) {
        padding = n;
    }

    public void clearResources() {
        resources.clear();
        population = -1;
    }

    public void addResource(Resource r, int amt) {
        resources.add(new ResourceAmount(r, amt));
    }

    public void addResource(ResourceAmount ra) {
        resources.add(ra);
    }

    public void addPopulation(int n) {
        population = n;
    }

    public void calculate() {
        int itm_width = padding + TEXT_WIDTH + ICON_WIDTH;
        if (resources.size() + population > 1) {
            itm_width *= 2;
        }
        this.setPreferredSize(new Dimension(itm_width + padding, PREF_HEIGHT * (((resources.size() - (population > 0 ? 0 : 1)) / 2) + 1)));
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        //int i = 0;
        FontMetrics fm = this.getFontMetrics(this.getFont());

        int base = ((PREF_HEIGHT - (fm.getAscent() + fm.getDescent())) / 2) + fm.getAscent() - fm.getDescent();

        //int lbly = ((PREF_HEIGHT - (fm.getAscent() + fm.getDescent())) / 2) + fm.getAscent() - fm.getDescent();
        //int icony = (PREF_HEIGHT - 20) / 2;

        int itm_width = padding + TEXT_WIDTH + ICON_WIDTH;

//        int lblx = this.getWidth() - (10 + TEXT_WIDTH);
//        int iconx = lblx - ICON_WIDTH;

        int i = 0;
        //System.out.println(this.getBaseline(200, 30) + ", " + fm.getAscent() + "  " + fm.getDescent());
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
        ArrayList<ResourceAmount> rcopy = new ArrayList<ResourceAmount>();
        rcopy.addAll(resources);
        for (ResourceAmount ra : rcopy) {
            if (ra.getAmount() < 0) {
                g2d.setColor(Color.RED);
            } else {
                g2d.setColor(Color.BLACK);
            }


            int lblx = this.getPreferredSize().width - TEXT_WIDTH - padding;

            int lbly = PREF_HEIGHT * (i / 2) + base;
            if (i % 2 == 1) {
                lblx -= itm_width;
            }

            int iconx = lblx - ICON_WIDTH;
            int icony = (PREF_HEIGHT * (i / 2)) + ((PREF_HEIGHT - ICON_WIDTH) / 2);

            g2d.drawImage(ra.getResource().getIcon(), iconx, icony, null);
            g2d.drawString(String.valueOf(ra.getAmount()), lblx, lbly);

//            lblx -= itm_width;
//            iconx -= itm_width;
            i++;
        }
        if (population >= 0) {
            int lblx = this.getWidth() - TEXT_WIDTH;

            int lbly = PREF_HEIGHT * (i / 2) + base;
            if (i % 2 == 1) {
                lblx -= itm_width;
            }

            int iconx = lblx - 20;
            int icony = (PREF_HEIGHT * (i / 2)) + ((PREF_HEIGHT - 20) / 2);
//            g2d.setColor(Color.red);
//            g2d.fillRect(iconx, icony, 20, 20);
            g2d.drawImage(Settlement.getPopulationImage(), iconx, icony, null);
//            System.out.println("draw pop");
//            g2d.setColor(Color.black);
            g2d.drawString(String.valueOf(population), lblx, lbly);
        }

        //g2d.drawString(label, 10, 20);

    }
}
